package manage.student;

import manage.handler.MainViewHandler;
import manage.req.StudentRequest;
import manage.res.TableDTO;
import manage.service.impl.StudentServiceImpl;
import manage.student.MainTableView.MainViewTable;
import manage.student.MainTableView.MainViewTableModel;
import manage.util.DimensionUtil;

import javax.swing.*;
import java.awt.*;

public class MainView extends JFrame {
    //北部
    JPanel northPanel=new JPanel();
    JButton addBtn=new JButton("添加");

    JButton updateBtn=new JButton("修改");
    JButton deleteBtn=new JButton("删除");
    JTextField searchText=new JTextField(15);
    JButton searchBtn=new JButton("搜索");
    //中部
    JPanel centerPanel=new JPanel();
    MainViewTable mainViewTable=new MainViewTable();
    //南部
    JPanel southPanel=new JPanel();
    JButton preBtn=new JButton("上一页");
    JButton nextBtn=new JButton("下一页");

    //表
    private  int pageNow=1;//当前页数
    private   int pageSize=10;//每页最多能显示10行
    private int pageStart=1;//分页查询时的起始位置



    MainViewHandler mainViewHandler;


    public MainView(){
        super("学生管理系统");
        Container container=getContentPane();
        mainViewHandler=new MainViewHandler(this);
        Rectangle bounds=DimensionUtil.getBounds();
        pageSize=Math.floorDiv(bounds.height,35);
        //添加组件(北)
        layoutNorth(container);
        //设置中间table
        layoutCenter(container);
        //(南)
        layoutSouth(container);


        //JFrame设置
        //根据屏幕大小设置主界面大小
        setBounds(bounds);

        //设置屏幕大小
        //  setBounds(DimensionUtil.getBounds());
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setLocationRelativeTo(null);

        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true);

        }

    private void layoutCenter(Container container) {
        //数据
        TableDTO tableDTO=getTableDTO();
        MainViewTableModel mainViewTableModel=MainViewTableModel.assembleModel(tableDTO.getData());

       mainViewTable.setModel(mainViewTableModel);
        mainViewTable.RenderRule();
        JScrollPane jScrollPane = new JScrollPane(mainViewTable);
       container.add(jScrollPane,BorderLayout.CENTER);
       //上一页/下一页判断
        shoePreNext(tableDTO.getTotalCount());
    }

    private TableDTO getTableDTO() {
        StudentServiceImpl studentService=new StudentServiceImpl();
        StudentRequest studentRequest=new StudentRequest();
        studentRequest.setPageNow(pageNow);
        studentRequest.setPageSize(pageSize);
        studentRequest.setSearchKey(searchText.getText().trim());
        TableDTO tableDTO=studentService.retrieveStudents(studentRequest);
        return tableDTO;
    }

    private void layoutSouth(Container container) {
        preBtn.addActionListener(mainViewHandler);
        nextBtn.addActionListener(mainViewHandler);
        southPanel.add(preBtn);
        southPanel.add(nextBtn);
        container.add(southPanel,BorderLayout.SOUTH);
    }

    private void layoutNorth(Container container) {
        //添加监听事件
        addBtn.addActionListener(mainViewHandler);
        updateBtn.addActionListener(mainViewHandler);
        deleteBtn.addActionListener(mainViewHandler);
        searchBtn.addActionListener(mainViewHandler);

        northPanel.add(addBtn);
        northPanel.add(deleteBtn);
        northPanel.add(searchBtn);
        northPanel.add(updateBtn);
        northPanel.add(searchText);
        container.add(northPanel,BorderLayout.NORTH);
    }

    //判断是否显示上一页和下一页
    private void shoePreNext(int totalCount){
        if(pageNow==1){
            preBtn.setVisible(false);
        }else{
            preBtn.setVisible(true);
        }
        //求总共有多少页
        int pageCount=0;
        if(totalCount%pageSize==0){
            pageCount=totalCount/pageSize;
        }else {
            pageCount=totalCount/pageSize+1;
        }
        if(pageNow==pageCount){
            nextBtn.setVisible(false);
        }else{
            nextBtn.setVisible(true);
        }

    }

    public void reloadTable(){
        TableDTO tableDTO=getTableDTO();
        MainViewTableModel.updateModel(tableDTO.getData());
        mainViewTable.RenderRule();
        shoePreNext(tableDTO.getTotalCount());
    }


    public int[] getSelectedStudentIds(){
        int []selectedRows=mainViewTable.getSelectedRows();
        int[]ids=new int[selectedRows.length];
        for(int i=0;i<selectedRows.length;i++){
            int rowIndex=selectedRows[i];
            Object idObj=mainViewTable.getValueAt(rowIndex,0);
            ids[i]=Integer.valueOf(idObj.toString());
        }
        return ids;
    }

    public static void main(String[] args) {
        new MainView();
    }

    public int getPageNow() {
        return pageNow;
    }

    public void setPageNow(int pageNow) {
        this.pageNow = pageNow;
    }

}
